import myAxios from "utils/request";
import {
  DownloadRequestOptions,
  DataListWithPage,
  DataListWithPage2,
} from "#/request";
import myDownloadAxios from "utils/request/downloadIndex";
import type {
  RoleListItem,
  GetRoleListParams,
  GetUserMenuTree,
  OrganizationList,
  OrganizationId,
  GetUserListParams,
  UserListItem,
  GetLogListParams,
  LogListItem,
  UserMenuTreeParams,
} from "./model/manage";
import type {
  UpdateParams as RoleUpdateParams,
  EditForm,
} from "views/manage/role/types";
import type {
  UpdateParams as UserUpdateParams,
  AddParams as UserAddParams,
} from "views/manage/user/types";
import type {
  UpdateParams as OrgUpdateParams,
  AddParams as OrgAddParams,
} from "views/manage/organization/types";

// callback
export const loginCallback = (cb: string) => {
  return myAxios.get({
    url: "/loginCallback",
    params: {
      callback: cb,
    },
  });
};

// 角色管理
export const getRoleList = (params: GetRoleListParams) => {
  return myAxios.request<DataListWithPage<RoleListItem>>({
    url: "/sys/role/list",
    method: "post",
    data: params,
  });
};

export const updateRole = (data: RoleUpdateParams) => {
  return myAxios.request({
    url: "/sys/role/update",
    method: "post",
    data,
  });
};
export const addRole = (data: EditForm) => {
  return myAxios.request({
    url: "/sys/role/add",
    method: "post",
    data,
  });
};

export const deleteRole = (id: number) => {
  return myAxios.request({
    url: `/sys/role/delete/${id}`,
    method: "post",
  });
};

// 组织架构管理
export const getOrganizationList = () => {
  return myAxios.request<OrganizationList>({
    url: "/sys/custom/dept/tree",
    method: "get",
  });
};

export const downloadOrganization = (options: DownloadRequestOptions) => {
  return myDownloadAxios.request(
    {
      url: "/sys/custom/dept/export",
      method: "post",
    },
    options
  );
};

export const updateOrganization = (data: OrgUpdateParams) => {
  return myAxios.request({
    url: "/sys/custom/dept/update",
    method: "put",
    data,
  });
};

export const addOrganization = (data: OrgAddParams) => {
  return myAxios.request({
    url: "/sys/custom/dept/add",
    method: "post",
    data,
  });
};

export const deleteOrganization = (id: OrganizationId) => {
  return myAxios.request({
    url: `/sys/custom/dept/delete/${id}`,
    method: "delete",
  });
};

// 用户管理
export const getUserList = (data: GetUserListParams) => {
  return myAxios.request<DataListWithPage2<UserListItem>>({
    url: `/sys/user/listFromDept`,
    method: "post",
    data,
  });
};

export const downloadUser = (
  data: GetUserListParams,
  options: DownloadRequestOptions
) => {
  return myDownloadAxios.request(
    {
      url: "/sys/user/export",
      method: "post",
      data,
    },
    options
  );
};

export const deleteUser = (id: number) => {
  return myAxios.request({
    url: `/sys/user/delete/${id}`,
    method: "delete",
  });
};

export const updateUser = (data: UserUpdateParams) => {
  return myAxios.request({
    url: "/sys/user/update",
    method: "put",
    data,
  });
};

export const addUser = (data: UserAddParams) => {
  return myAxios.request({
    url: "/sys/user/add",
    method: "post",
    data,
  });
};

export const getAllRoleList = () => {
  return myAxios.request<RoleListItem[]>({
    url: "/sys/role/listAll",
    method: "get",
  });
};

// 日志管理
export const getLog = (data: GetLogListParams) => {
  return myAxios.request<DataListWithPage2<LogListItem>>({
    url: "/journal/excelDataList",
    method: "post",
    data,
  });
};

export const downloadLog = (
  data: GetLogListParams,
  options: DownloadRequestOptions
) => {
  return myDownloadAxios.request(
    {
      url: "/journal/exportJournalExcel",
      method: "post",
      data,
    },
    options
  );
};

// 用户菜单
export const getUserMenu = (params: UserMenuTreeParams) => {
  return myAxios.request<GetUserMenuTree>({
    url: "/sys/menu/getUserTree",
    method: "get",
    params,
  });
};
